package com.cloud.class05.controller;

import com.cloud.class05.apis.PayFeignApi;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

@RestController
public class OrderCircuitController {
    @Resource
    private PayFeignApi payFeignApi;

    @GetMapping(value = "/feign/pay/circuit/{id}")
    @CircuitBreaker(name = "cloud-payment-service", fallbackMethod = "myCircuitFallback")
    public String myCircuitBreaker(@PathVariable("id") Integer id) {
        return payFeignApi.myCircuit(id);
    }

    //myCircuitFallback就是服务降级后的兜底处理方法
    public String myCircuitFallback(Integer id, Throwable t) {
        // 这里是容错处理逻辑，返回备用结果
        return "myCircuitFallback，系统繁忙，请稍后再试-----/(ㄒoㄒ)/~~";
    }


    //    测试舱壁，隔离
    @GetMapping(value = "/feign/pay/bulkhead/{id}")
//    Bulkhead次注解的作用是对舱壁进行隔离，防止舱壁内线程过多导致系统崩溃，注解中的参数name是必填项，用于标识舱壁的名称，
//    fallbackMethod是可选项，用于指定舱壁内发生异常时的处理方法
    @Bulkhead(name = "cloud-payment-service", fallbackMethod = "myBulkheadFallback",type = Bulkhead.Type.SEMAPHORE)
    public String myBulkhead(@PathVariable("id") Integer id) {
        return payFeignApi.myBulkhead(id);
    }

    public String myBulkheadFallback(Integer id, Throwable t) {
        // 这里是容错处理逻辑，返回备用结果
        return "myCircuitFallback，隔板超出最大数量限制，系统繁忙，请稍后再试-----";
    }


    /**
     * (船的)舱壁,隔离,THREADPOOL
     *
     * @param id
     * @return
     */
//    @GetMapping(value = "/feign/pay/bulkhead/{id}")
//    @Bulkhead(name = "cloud-payment-service", fallbackMethod = "myBulkheadPoolFallback", type = Bulkhead.Type.THREADPOOL)
//    public CompletableFuture<String> myBulkheadTHREADPOOL(@PathVariable("id") Integer id) {
//        System.out.println(Thread.currentThread().getName() + "\t" + "enter the method!!!");
//        try {
//            TimeUnit.SECONDS.sleep(5);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
////        CompletableFuture.supplyAsync(new Supplier<String>() {
////
////            @Override
////            public String get() {
////                return  payFeignApi.myBulkhead(id) + "\t" + " Bulkhead.Type.THREADPOOL";
////            }
////        });
//        System.out.println(Thread.currentThread().getName() + "\t" + "exist the method!!!");
////        supplyAsync()方法返回的是一个CompletableFuture对象，该对象封装了异步执行的结果，可以通过get()方法获取结果
//        return CompletableFuture.supplyAsync(() -> payFeignApi.myBulkhead(id) + "\t" + " Bulkhead.Type.THREADPOOL");
//    }
//
//    public CompletableFuture<String> myBulkheadPoolFallback(Integer id, Throwable t) {
//        return CompletableFuture.supplyAsync(() -> "Bulkhead.Type.THREADPOOL，系统繁忙，请稍后再试-----/(ㄒoㄒ)/~~");
//    }


}
